package 数据库.练习1;

import 数据库.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class SubjectDao {
    public int add(Subject s) {
        Connection connection = null;
        PreparedStatement st = null;
        try {
            connection = DBUtil.getConnection();
            Scanner sc = new Scanner(System.in);

            String sql = "insert into s_subject(subjectname,classhour,gradeid) values(?,?,?)";
            System.out.println(sql);
            st = connection.prepareStatement(sql);
            st.setString(1, s.getSubjectname());
            st.setInt(2, s.getClasshour());
            st.setLong(3, s.getGradeid());

            return st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(null, st, connection);
        }
        return 0;
    }

    public int update(Subject s) {
        Connection connection = null;
        PreparedStatement st = null;
        try {
            connection = DBUtil.getConnection();
            Scanner sc = new Scanner(System.in);

            String sql = "UPDATE  s_subject  SET  subjectname=?,classhour=?,gradeid=? where  id=?";
            System.out.println(sql);
            st = connection.prepareStatement(sql);
            st.setString(1, s.getSubjectname());
            st.setInt(2, s.getClasshour());
            st.setLong(3, s.getGradeid());
            st.setLong(4, s.getId());
            return st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(null, st, connection);
        }
        return 0;
    }

    public int del(long id) {
        Connection connection = null;
        PreparedStatement st = null;
        try {
            connection = DBUtil.getConnection();
            Scanner sc = new Scanner(System.in);

            String sql = "delete   from s_subject  where  id=?";
            System.out.println(sql);
            st = connection.prepareStatement(sql);
            st.setLong(1, id);
            return st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(null, st, connection);
        }
        return 0;
    }

    //主键找对象
    public Subject get(long id) {
        Connection connection = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        Subject s=null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from  s_subject  where  id=?";
            System.out.println(sql);
            st = connection.prepareStatement(sql);
            st.setLong(1, id);
            rs = st.executeQuery();
            if (rs.next()){
                s=new Subject(rs.getLong("id"),rs.getString("subjectname"),rs.getInt("classhour"),rs.getLong("gradeid"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(null, st, connection);
        }
        return null;
    }

    //查询全部对象
    public List<Subject> findALL() {
        return null;
    }

    public List<Subject> search(int pageNo,int pageSize) {
        int i=(pageNo-1)*pageSize;
        String sql = "select * from s_subject   order by id asc   limit ?,?";
        List<Subject> datas = new ArrayList<>();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = DBUtil.getConnection();
            ps = con.prepareStatement(sql);
            ps.setInt(1, i);
            ps.setInt(2, pageSize);
            rs = ps.executeQuery();
            while (rs.next()) {
                datas.add(new Subject(rs.getLong("id"), rs.getString("subjectname"), rs.getInt("classhour"), rs.getLong("gradeid")));
            }
        } catch (SQLException e) {
           // DBUtil.handlerEx(e);
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, ps, con);
        }
        return datas;
    }
    public static int executeUpdate(String sql,Object...args){
        Connection connection = null;
        PreparedStatement st = null;
        try {
            connection = DBUtil.getConnection();
            System.out.println(sql);
            st = connection.prepareStatement(sql);
            for (int i=0;i<args.length;i++)
                st.setObject(i+1,args[i]);
            return st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(null, st, connection);
        }
        return 0;
    }

}

